package com.jml.zlhq.app.ui;

import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppConfig;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.CommonDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class SettingActivity extends BaseActivity {
	private TextView help,surgest,kefu,about,weixin,weibo,clear;
	private Button logout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_activity);
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.back_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.GONE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("设置");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setVisibility(View.INVISIBLE);
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
		
		help = (TextView) findViewById(R.id.setting_help);
		surgest = (TextView) findViewById(R.id.setting_surgest);
		kefu = (TextView) findViewById(R.id.setting_kefu);
		about = (TextView) findViewById(R.id.setting_about);
		logout = (Button) findViewById(R.id.setting_logout);
		weixin = (TextView) findViewById(R.id.setting_weixin);
		weibo = (TextView) findViewById(R.id.setting_weibo);
		clear = (TextView) findViewById(R.id.setting_clear);
		help.setOnClickListener(this);
		surgest.setOnClickListener(this);
		kefu.setOnClickListener(this);
		about.setOnClickListener(this);
		weixin.setOnClickListener(this);
		weibo.setOnClickListener(this);
		clear.setOnClickListener(this);
		logout.setOnClickListener(this);
		logout.setVisibility(appContext.isLogin()?View.VISIBLE:View.GONE);
	}

	@Override
	public void loadData(Map<String, String> param) {}

	@Override
	public void clearData() {}
	
	private long getFileSize(File f) {
		long size = 0;
		File flist[] = f.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getFileSize(flist[i]);
			} else {
				size = size + flist[i].length();
			}
		}
		return size;
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.setting_kefu:
			Intent intent=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:028-85351903"));
			startActivity(intent);
			break;
		case R.id.setting_help:
			Bundle help = new Bundle();
			help.putString("url", URLs.APP_HELP);
			UIUtil.openActivity(activity, CommonWebActivity.class, help);
			break;
		case R.id.content_header_left_lay:
			finish();
			break;
		case R.id.setting_surgest:
			UIUtil.openActivity(this, SurgestActivity.class, null);
			break;
		case R.id.setting_logout:
			appContext.Logout(SettingActivity.class.getName());
			logout.setVisibility(appContext.isLogin()?View.VISIBLE:View.GONE);
			WodeFragment.freshPage = true;
			finish();
			break;
		case R.id.setting_about:
			UIUtil.openActivity(activity, AboutActivity.class, null);
			break;
		case R.id.setting_clear:
			File images = StorageUtils.getOwnCacheDirectory(getApplicationContext(), AppConfig.APP_IMAGE);
			BigDecimal b = new BigDecimal((float)getFileSize(images)/(float)(1024*1024));  
			float f1 = b.setScale(2,   BigDecimal.ROUND_HALF_UP).floatValue();  
			ImageLoader.getInstance().clearDiskCache();
			ImageLoader.getInstance().clearMemoryCache();
			appContext.clearAppCache();
			UIUtil.ToastMessage(activity, "清除缓存 ("+f1+"M)");
			WodeFragment.freshPage = true;
			break;
		case R.id.setting_weixin:
			final CommonDialog dialog = new CommonDialog(activity, true);
			dialog.show(true, true, false, "赚零花钱微信号", appContext.getWeiboWeixin().getWeixinUrl(), "复制", "", new OnClickListener() {
				@Override
				public void onClick(View v) {
					ClipboardManager c= (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
					ClipData clip = ClipData.newPlainText("weixin", appContext.getWeiboWeixin().getWeixinUrl());
					c.setPrimaryClip(clip);
					UIUtil.ToastMessage(activity, "复制成功");
					dialog.dismiss();
				}
			}, null);
			break;
		case R.id.setting_weibo:
			@SuppressWarnings("unused")
			String weibo = appContext.getWeiboWeixin().getWeiboUrl();
			//follow_frinends(weibo);
			Bundle data = new Bundle();
			data.putString("url", "http://m.weibo.cn/d/weddingschina");
			UIUtil.openActivity(activity, CommonWebActivity.class, data);
			break;
		default:
			break;
		}
	}
	
	//消息处理机制
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 3: {
				Toast.makeText(SettingActivity.this, "关注“"+AppConfig.weibo+"”失败", Toast.LENGTH_SHORT).show();
			}break;
			
			case 1:{
				Toast.makeText(SettingActivity.this, "关注“"+AppConfig.weibo+"”成功", Toast.LENGTH_SHORT).show();
			}break;
			
			case 2:{
				Toast.makeText(SettingActivity.this, "取消关注“"+AppConfig.weibo, Toast.LENGTH_SHORT).show();
			}break;
			}
		}
	};
	
	//关注某个好友
	public void follow_frinends(String name){
			Platform weibo=ShareSDK.getPlatform(SinaWeibo.NAME);
			weibo.SSOSetting(true);//禁用SSO授权
			weibo.setPlatformActionListener(new PlatformActionListener() {

				public void onError(Platform arg0, int arg1, Throwable arg2) {
					arg0.removeAccount();
					Message msg=new Message();
					msg.what=3;
					handler.sendMessage(msg);
				}
				public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
					Message msg=new Message();
					msg.what=1;
					handler.sendMessage(msg);
				}
				public void onCancel(Platform arg0, int arg1) {
					Message msg=new Message();
					msg.what=2;
					handler.sendMessage(msg);
				}
			});
			//需要关注好友的微博名字
			weibo.followFriend("赚零花钱");
	}

}
